import 'package:flutter/material.dart';

import '../../api/house.dart';
import '../../utils/EventBus.dart';
import '../../utils/LoadingDialog.dart';
import '../../utils/PromptAction.dart';

class HouseDetail extends StatefulWidget {
  const HouseDetail({super.key, required this.id});
  final String id;

  @override
  State<HouseDetail> createState() => _HouseDetailState();
}

class _HouseDetailState extends State<HouseDetail> {
  Widget tagBuilder(int status) {
    List tagList = [
      {},
      {
        "bgColor": const Color.fromARGB(50, 91, 177, 227),
        "textColor": const Color.fromARGB(255, 85, 145, 175),
        "title": "审核中"
      },
      {
        "bgColor": const Color.fromARGB(255, 91, 243, 91),
        "textColor": const Color.fromRGBO(1, 50, 1, 1),
        "title": "审核成功"
      },
      {
        "bgColor": const Color.fromARGB(255, 247, 129, 133),
        "textColor": const Color.fromARGB(255, 84, 1, 1),
        "title": "审核失败"
      }
    ];

    return Container(
        padding: const EdgeInsets.only(left: 5, right: 5, top: 2, bottom: 2),
        decoration: BoxDecoration(
            color: tagList[status]['bgColor'],
            borderRadius: BorderRadius.circular(5)),
        child: Text('${tagList[status]['title']}',
            style: TextStyle(color: tagList[status]['textColor'])));
  }

  Map<String, dynamic>? house;
  @override
  void initState() {
    super.initState();
    if (!widget.id.isEmpty) {
      _getHouseDetail();
    }
  }

  // _getHouseDetail() {
  //   // widget.id  发请求
  //   // 封装api函数
  //   // getHouseDetailAPI(widget.id).then((value) {
  //   // });
  // }

  void _getHouseDetail() async {
    var res = await getHouseDetailAPI(widget.id);
    house = res;
    house!['gender'] = '${house!['gender']}'; // 强行把后端返回的gender改字符串
    print('---');
    print(house);
    print('---');

    setState(() {});
  }

  // Widget getLoadingWidget(BuildContext context,
  //     {String? message = "数据加载中..."}) {
  //   return Center(
  //     child: Column(
  //       mainAxisAlignment: MainAxisAlignment.center,
  //       children: [
  //         const CircularProgressIndicator(
  //           color: Colors.blue,
  //         ),
  //         const SizedBox(height: 20),
  //         Text(
  //           message!,
  //           style: const TextStyle(color: Colors.grey, fontSize: 12),
  //         )
  //       ],
  //     ),
  //   );
  // }

  void _delHouse() {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: const Text("提示消息"),
            content: const Text("确认删除该房屋吗"),
            actions: [
              TextButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: const Text(
                    "取消",
                    style: TextStyle(color: Colors.red),
                  )),
              TextButton(
                  onPressed: () async {
                    await delHouseAPI(widget.id);
                    PromptAction.sucess("删除成功");
                    eventBus.fire(RefreshEvent());
                    Navigator.pop(context); // 关闭弹窗
                    Navigator.pop(context); // 返回上一页
                  },
                  child: const Text(
                    "确认",
                  ))
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('房源详情'),
          centerTitle: true,
        ),
        body: house == null
            ? getLoadingWidget(context)
            : Stack(
                alignment: Alignment.bottomCenter,
                children: [
                  ListView(
                    padding: const EdgeInsets.only(bottom: 100),
                    children: [
                      // 房屋信息
                      Container(
                        padding: const EdgeInsets.all(10),
                        child: const Text('房屋信息',
                            style: TextStyle(
                                color: Color.fromARGB(255, 97, 94, 94),
                                fontSize: 16)),
                      ),
                      Container(
                          color: Colors.white,
                          padding: const EdgeInsets.only(
                              left: 10, right: 10, top: 15, bottom: 15),
                          child: Row(
                            children: [
                              Expanded(
                                  child: Text(
                                      house!['point'] + house!['building'])),
                              tagBuilder(1)
                            ],
                          )),
                      // 业主信息
                      Container(
                        padding: const EdgeInsets.all(10),
                        child: const Text('业主信息',
                            style: TextStyle(
                                color: Color.fromARGB(255, 97, 94, 94),
                                fontSize: 16)),
                      ),
                      Container(
                          color: Colors.white,
                          padding: const EdgeInsets.only(
                              left: 10, right: 10, top: 15, bottom: 15),
                          child: Row(
                            children: [
                              SizedBox(
                                width: 60,
                                child: Text('房间号'),
                              ),
                              SizedBox(width: 30),
                              Expanded(child: Text(house!['room']))
                            ],
                          )),
                      Container(
                          color: Colors.white,
                          padding: const EdgeInsets.only(
                              left: 10, right: 10, top: 15, bottom: 15),
                          child: Row(
                            children: [
                              SizedBox(
                                width: 60,
                                child: Text('业主'),
                              ),
                              SizedBox(width: 30),
                              Expanded(child: Text(house!['name']))
                            ],
                          )),
                      Container(
                          color: Colors.white,
                          padding: const EdgeInsets.only(
                              left: 10, right: 10, top: 15, bottom: 15),
                          child: Row(
                            children: [
                              SizedBox(
                                width: 60,
                                child: Text('手机号'),
                              ),
                              SizedBox(width: 30),
                              Expanded(child: Text(house!['mobile']))
                            ],
                          )),
                      const SizedBox(height: 30),
                      Container(
                          padding: const EdgeInsets.all(10),
                          color: Colors.white,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              const Text('本人身份证照片'),
                              Image.network(house!['idcardFrontUrl'],
                                  height: 200),
                              Image.network(house!['idcardBackUrl'],
                                  height: 200)
                            ],
                          )),
                    ],
                  ),
                  Container(
                      padding: const EdgeInsets.all(10),
                      width: MediaQuery.of(context).size.width,
                      height: 80,
                      child: Row(
                          mainAxisAlignment: MainAxisAlignment.spaceAround,
                          children: [
                            ElevatedButton(
                                onPressed: () {
                                  _delHouse();
                                },
                                child:  Column(
                                  children: [
                                    SizedBox(
                                      height: 8,
                                      width: 120,
                                    ),
                                    Icon(Icons.delete),
                                    Text('删除房屋'),
                                    SizedBox(height: 8)
                                  ],
                                )),
                            ElevatedButton(
                                onPressed: () {
                                  Navigator.pushNamed(context, '/house_form',
                                      arguments: house);
                                },
                                child:  Column(
                                  children: [
                                    SizedBox(
                                      height: 8,
                                      width: 120,
                                    ),
                                    Icon(Icons.edit),
                                    Text('修改房屋'),
                                    SizedBox(height: 8)
                                  ],
                                ))
                          ]))
                ],
              ));
  }
}
